package tz.preprocessing;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

import tz.data.Article;

public class Choose10Topics {
	
	private HashSet<String> commonTopics = new HashSet<String>();
	private HashMap<String, Integer> commonTopicsOccurrence = new HashMap<String, Integer>();
	
	public Choose10Topics(){
		this.fillComonTopics();
	}
	
	public void fillComonTopics(){
		commonTopics.add("acq");
		commonTopics.add("corn");
		commonTopics.add("crude");
		commonTopics.add("earn");
		commonTopics.add("grain");
		commonTopics.add("interest");
		commonTopics.add("money-fx");
		commonTopics.add("ship");
		commonTopics.add("trade");
		commonTopics.add("wheat");
	
	}
	public boolean contains(String s){
		if (this.commonTopics.contains(s)){
			Integer i = commonTopicsOccurrence.get(s);
			if(i != null){
				i++;
				commonTopicsOccurrence.put(s, i);
			}else{
				i=1;
				commonTopicsOccurrence.put(s, i);
			}
			return true;
		}
		return false;
	}
	
	public int checkOccurrence(String topic){
		Integer numOfO = commonTopicsOccurrence.get(topic);
		if(numOfO != null){
			return numOfO;
		}
		return 0;
	}
	
	/*public SortedMap<Integer, LinkedList<String>> GetTop30Topics(List<Article> lstArticles){
		int numOfArticlesAdded = 0;
		for(int i = 0; i < 4000; i++){
			if(lstArticles.get(i).hasTopics() == false){
				continue;
			}
			
			
			Article art = lstArticles.get(i);
		
			
			numOfArticlesAdded++;
			//to vrstico spreminjamo glede na stevilo primerov ki hocemo dobit
			//if(numOfArticlesAdded>200) break;  
			
			SortedMap<Integer, LinkedList<String>> m = TopTopics.tailMap(10);
			
			if (numOfArticles(m)){
				return m;
				
			}
			String articleTopic = art.getArticleTopics();
			LinkedList<Integer> IdTopics = clustersTopics.get(articleTopic);
			if( IdTopics != null){
				IdTopics.add(art.getNewId());			
				clustersTopics.put(articleTopic, IdTopics);
			}else{
				IdTopics = new LinkedList<Integer>();
				IdTopics.add(art.getNewId());
				clustersTopics.put(articleTopic, IdTopics);
			}
			
			
			topTopicsPutNew(articleTopic);
			
			//System.out.println("ID "+art.getArticleID()+", TOPICS "+art.getArticleTopics());
			}
		
			return null;
		
		
		}
		
		
		private void topTopicsPut(Integer val, String topicName){
			LinkedList<String> topics = this.TopTopics.get(val);
			if(topics == null){
				topics = new LinkedList<String>();
				topics.add(topicName);
				
			}else{
				topics.add(topicName);
			}
			TopTopics.put(val, topics);
			
		}
		
		private static void topTopicsPutNew(String topicName){
				LinkedList<Integer> articleIds = clustersTopics.get(topicName);
				int numOfArticles = articleIds.size();
				
				LinkedList<String> topicsWithTheSameSize = TopTopics.get(numOfArticles-1);
				LinkedList<String> topicsWithTheSameNewSize = TopTopics.get(numOfArticles);

				if(topicsWithTheSameSize!=null){
					if(topicsWithTheSameSize.remove(topicName)){
						
						if(topicsWithTheSameNewSize==null){
							topicsWithTheSameNewSize = new LinkedList<String>();
							TopTopics.put(numOfArticles, topicsWithTheSameNewSize);
						}
						
						topicsWithTheSameNewSize.add(topicName);
				
					}else{
						System.out.println("NAPAKA: Choose10Topics, topTopicsPutNew 1, return false on removing element");
					}
				}else System.out.println("NAPAKA Choose10Topics, topTopicsPutNew 1, topic NULL");
				
				
				
		}
	
		private static boolean numOfArticles(SortedMap<Integer, LinkedList<String>> m){
			int sumOfArticles = 0;
			for( Integer i  : m.keySet()){
				System.out.println(i);
				sumOfArticles = sumOfArticles + i;
				if(i>=numOfTopicsToBeChosen){
					end = new Integer(i);
					return true;
				}
			}
			return false;
		}*/
}
